package org.hegang.design.first.prototypepattern;


import java.util.Hashtable;

/**
 * @ClassName ShapeCache
 * @Describe: 形状对象缓存
 * @Author: gang.he
 * @Email: SmileSkylife@outlook.com
 * @Date: Created in 23:20 2019/7/3
 * @Modified_By: TODO
 * @Version: V1.0
 */
public class ShapeCache {
    /**
     * 缓存对象Hashtable
     */
    private static Hashtable<String, AbstractShape> shapeMap = new Hashtable<>();

    /**
     * 从缓存对象中克隆对象，减少创建对象的代价
     * @param abstractShapeId
     * @return
     */
    public static AbstractShape getAbstractShape(String abstractShapeId){
        AbstractShape cacheAbstractShape = shapeMap.get(abstractShapeId);
        return (AbstractShape)cacheAbstractShape.clone();
    }

    /**
     * 从数据库加载对象，简单处理
     */
    public static void loadCache(){
        //从数据库加载矩形，简单处理
        Rectangle rectangle = new Rectangle();
        rectangle.setId("1");
        shapeMap.put(rectangle.getId(),rectangle);

        Circle circle = new Circle();
        circle.setId("2");
        shapeMap.put(circle.getId(),circle);

        Square square = new Square();
        square.setId("3");
        shapeMap.put(square.getId(),square);
    }
}
